package cn.nexd.utils;

import android.content.Context;
import android.util.Log;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import cn.nexd.utils.framework.tools.FileUtil;
import cn.nexd.utils.framework.tools.SDCardUtil;

/**
 * Created by wangxu on 16/11/8.
 */

public class MainPresenter {


    private static final String TAG = "MainPresenter";


    private IMainView iMainView;
    private Context context;

    MainPresenter(IMainView iMainView, Context context) {
        this.iMainView = iMainView;
        this.context = context;
    }






    private boolean compressFolderChangeToZip(File compressedFile, ZipArchiveOutputStream zipArchiveOutputStream, String base) throws IOException {
        FileInputStream fileInputStream = null;
        try {
            if (compressedFile.isDirectory()) {
                File[] childrenCompressedFileList = compressedFile.listFiles();
                base = base.length() == 0 ? "" : base + File.separator;
                for (File file : childrenCompressedFileList) {
                    compressFolderChangeToZip(file, zipArchiveOutputStream, base + file.getName());
                }
            } else {
                if ("".equalsIgnoreCase(base)) {
                    base = compressedFile.getName();
                }
                Log.d(TAG, "compressFolderChangeToZip:  file name = " + compressedFile.getName() + "\n file size = " + FileUtil.getFileSize(compressedFile.getPath(), FileUtil.SIZE_TYPE_MB));
                zipArchiveOutputStream.putArchiveEntry(new ZipArchiveEntry(base));
                fileInputStream = new FileInputStream(compressedFile);
                int b;

                byte[] bytes = new byte[8192];
                BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream, 8192);
                while ((b = bufferedInputStream.read(bytes)) != -1) {
                    zipArchiveOutputStream.write(bytes, 0, b);
                }

                bufferedInputStream.close();
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }


    private static boolean compressFloderChangeToZip(File compressedFile, ZipOutputStream zipOutputStream, String base)
            throws IOException {
        FileInputStream fileInputStream = null;

        try {
            if (compressedFile.isDirectory()) {
                File[] childrenCompressedFileList = compressedFile.listFiles();
                base = base.length() == 0 ? "" : base + File.separator;
                for (int i = 0; i < childrenCompressedFileList.length; i++) {
                    compressFloderChangeToZip(childrenCompressedFileList[i],
                            zipOutputStream, base + childrenCompressedFileList[i].getName());
                }
            } else {
                if ("".equalsIgnoreCase(base)) {
                    base = compressedFile.getName();
                }
                zipOutputStream.putNextEntry(new ZipEntry(base));
                fileInputStream = new FileInputStream(compressedFile);
                int b;
                while ((b = fileInputStream.read()) != -1) {
                    zipOutputStream.write(b);
                }
                fileInputStream.close();
            }
            return true;
        } catch (Exception e) {
            e.getStackTrace();
            return false;
        }
    }

}
